﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace FirstFloor.P2P
{
    /// <summary>
    /// Provides the list of participating users.
    /// </summary>
    public class P2PUserList
        : IEnumerable<P2PUser>
    {
        private List<P2PUser> users = new List<P2PUser>();
        private P2PUser me;
        private P2PUser inviter;

        /// <summary>
        /// Initializes a new instance of the <see cref="P2PUserList"/> class.
        /// </summary>
        internal P2PUserList()
        {
        }

        /// <summary>
        /// Adds a user.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="globalIp">The global ip.</param>
        /// <param name="localIp">The local ip.</param>
        /// <param name="name">The name.</param>
        /// <param name="puid">The puid.</param>
        /// <param name="isMe">if set to <c>true</c> [is me].</param>
        /// <param name="isInviter">if set to <c>true</c> [is inviter].</param>
        internal void Add(string email, string globalIp, string localIp, string name, string puid, bool isMe, bool isInviter)
        {
            P2PUser user = new P2PUser(email, globalIp, localIp, name, puid, isMe, isInviter);

            this.users.Add(user);

            if (isMe) {
                this.me = user;
            }
            if (isInviter){
                this.inviter = user;
            }
        }

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <value>The count.</value>
        public int Count
        {
            get { return this.users.Count; }
        }

        /// <summary>
        /// Gets the <see cref="FirstFloor.P2P.P2PUser"/> at the specified index.
        /// </summary>
        /// <value></value>
        public P2PUser this[int index]
        {
            get { return this.users[index]; }
        }

        /// <summary>
        /// Gets the local user.
        /// </summary>
        /// <value>Me.</value>
        public P2PUser Me
        {
            get { return this.me; }
        }

        /// <summary>
        /// Gets the inviter.
        /// </summary>
        /// <value>The inviter.</value>
        public P2PUser Inviter
        {
            get { return this.inviter; }
        }

        #region IEnumerable<P2PUser> Members

        public IEnumerator<P2PUser> GetEnumerator()
        {
            return this.users.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator IEnumerable.GetEnumerator()
        {
            return this.users.GetEnumerator();
        }

        #endregion
    }
}
